package cn.edu.swufe.surverpark.util;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.net.URL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.swufe.surverpark.struts2.service.RightService;

/**
 * @author 陈龙 E-mail:529914446@qq.com
 * @version创建时间:2015年5月6日上午4:25:42 类说明
 */
public class ExtractAllRightsUtil {
	
	public static void main(String[] args) throws URISyntaxException {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		RightService rs = (RightService) ac.getBean("rightService");
		ClassLoader loader = ExtractAllRightsUtil.class.getClassLoader();
		URL url = loader.getResource("cn/edu/swufe/surverpark/struts2/action");
		File dir = new File(url.toURI());
		File[] files = dir.listFiles();
		String fname = "";
		for (File f : files) {
			fname = f.getName();
			if (fname.endsWith(".class") && !fname.equals("BaseAction.class")) {
				processAction(fname,rs);
			}
		}
	}

	private static void processAction(String fname,RightService rs) {
		try {
			String pkgName = "cn.edu.swufe.surverpark.struts2.action";
			String simpleClassName = fname
					.substring(0, fname.indexOf(".class"));
			String className = pkgName + "." + simpleClassName;

			Class clazz = Class.forName(className);
			Method[] methods = clazz.getDeclaredMethods();
			Class retType = null;
			String mname = null;
			Class[] paramType = null;
			String url = null;
			for(Method m :methods){
				retType = m.getReturnType();
				mname = m.getName();
				paramType = m.getParameterTypes();
				if(retType == String.class
						&& !ValidateUtil.isValid(paramType)
						&& Modifier.isPublic(m.getModifiers())){
					if(mname.equals("execute")){
						url = "/" + simpleClassName;
					}else{
						url = "/" + simpleClassName + "_" + mname;
					}
					rs.appendRightByURL(url);
				}
			}
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
	}
}
